package com.example.lib_apply.detail

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.example.lib_apply.ApplyEntity
import com.example.lib_apply.databinding.ActivityDetaliBinding
import com.example.lib_common.mvi.BaseActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

@Route(path = "/apply/DetailActivity")
class DetailActivity : BaseActivity<ActivityDetaliBinding,DetailViewModel>() {
    override fun createVM(): DetailViewModel {
        return DetailViewModel()
    }

    override fun createViewBinding(): ViewBinding {
        return ActivityDetaliBinding.inflate(inflater)
    }
    var visitorList : ArrayList<VisitorEntity> =ArrayList()
    var parkingList : ArrayList<ParkingEntity> =ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val applyEntity = intent.getSerializableExtra("visitor") as ApplyEntity
        val whereW = intent.getStringExtra("where")
        val map =HashMap<String,Any>()
        when (whereW) {
            "来访" -> {
                map["time"]=applyEntity.applyTime.toString()
                map["userId"]=SPUtils.getInstance().getInt("uId")
                map["vistorId"]=applyEntity.visitorId
                vm.visitorStep(map)
                v.detailParking.layoutParking.visibility=View.GONE
                v.detailVisitor.layoutVisitor.visibility=View.VISIBLE
            }
            "车位" -> {
                map["parkId"]=applyEntity.parkId
                map["parkUser"]=SPUtils.getInstance().getInt("uId")
                map["time"]=applyEntity.applyTime.toString()
                v.detailVisitor.layoutVisitor.visibility=View.GONE
                v.detailParking.layoutParking.visibility=View.VISIBLE
                vm.parkingStep(map)
            }
        }
        bindState()
    }
    val  startCalendar= Calendar.getInstance()
    val  endCalendar= Calendar.getInstance()
    val  timeCalendar= Calendar.getInstance()
    var year = 0
    var month = 0
    var day = 0
    var startMonth = 0
    var starDay = 0
    var endMonth = 0
    var endDay = 0
    private fun bindDate() {
        LogUtils.e("======详情:${parkingList.size},${visitorList.size}")
        if (parkingList!=null){
            try {
                val mStart = parkingList[0].parkStart!!.toLong()
                val mEnd = parkingList[0].parkEnd!!.toLong()
                val mTime = parkingList[0].parkTime!!.toLong()
                changeDate(mStart,mEnd,mTime)
                v.detailParking.startDate.text=""+startMonth+"月"+starDay+"日"
                v.detailParking.endDate.text=""+endMonth+"月"+endDay+"日"
                v.detailParking.userName.text=parkingList[0].parkName
                v.detailParking.belong.text=SPUtils.getInstance().getString("department")
                v.detailParking.applyDate.text=""+year+"年"+month+"月"+day+"日"
            }catch (e : Exception){

            }
        }else if (visitorList!=null){
            try {
                val mStart = visitorList[0].visitorStart!!.toLong()
                val mEnd = visitorList[0].visitorEnd!!.toLong()
                val mTime = visitorList[0].visitorTime!!.toLong()
                changeDate(mStart,mEnd,mTime)
                v.detailVisitor.visitorStart.text=""+startMonth+"月"+starDay+"日"
                v.detailVisitor.visitorEnd.text=""+endMonth+"月"+endDay+"日"
                v.detailVisitor.visitorName.text=SPUtils.getInstance().getString("uName")
                v.detailVisitor.visitorDepartment.text=SPUtils.getInstance().getString("department")
                v.detailVisitor.visitorDate.text=""+year+"年"+month+"月"+day+"日"
                v.detailVisitor.isCar.isChecked=true
                v.detailVisitor.visitorCarNum.setText(visitorList[0].carNum)
                v.detailVisitor.parkId.text=""+visitorList[0].parkId
                v.detailVisitor.visitorDescribe.setText(visitorList[0].visitorDescribe)
            }catch (e : Exception){

            }
        }
    }

    private fun changeDate(mStart: Long, mEnd: Long, mTime: Long) {
        startCalendar.timeInMillis=mStart
        endCalendar.timeInMillis=mEnd
        timeCalendar.timeInMillis=mTime
        year=timeCalendar.get(Calendar.YEAR)
        month = timeCalendar.get(Calendar.MONTH)
        day = timeCalendar.get(Calendar.DAY_OF_MONTH)
        startMonth = startCalendar.get(Calendar.MONTH)
        starDay = startCalendar.get(Calendar.DAY_OF_MONTH)
        endMonth = endCalendar.get(Calendar.MONTH)
        endDay = endCalendar.get(Calendar.DAY_OF_MONTH)
    }


    private fun bindState() {
        lifecycleScope.launch(Dispatchers.Main){
            repeatOnLifecycle(Lifecycle.State.STARTED){
                vm.mutableContainer.uiStateFlow.collect{
                    LogUtils.e("======状态${it.entity.statuesCode}")
                    if (it.entity.statuesCode==200){
                        parkingList= it.entity.values as ArrayList<ParkingEntity>
                        LogUtils.e("======赋值${parkingList.size},${parkingList[0].parkName}")
                        bindDate()
                    }
                    if (it.entity1.statuesCode==200){
                        visitorList= it.entity1.values as ArrayList<VisitorEntity>
                        bindDate()
                    }
                }
            }
        }
    }
}